﻿using System;
using System.Net;
using System.Text;

namespace FluentWebRequest
{
    internal class PostRequestMethodBuilder : IRequestMethodBuilder
    {
        private readonly string _url;
        private readonly string _parametters;
        private HttpWebRequest _webRequest;

        public PostRequestMethodBuilder(HttpWebRequest webRequest, string url, string parametters)
        {
            _webRequest = webRequest;
            _url = url;
            _parametters = parametters;
        }

        public HttpWebRequest GetDataFromUri()
        {
            var uri = new Uri(_url);
            _webRequest = (HttpWebRequest)WebRequest.Create(uri);
            _webRequest.Method = "POST";
            _webRequest.ContentType = "application/x-www-form-urlencoded";
            _webRequest.ContentLength = _parametters.Length;
            using (var writeStream = _webRequest.GetRequestStream())
            {
                var encoding = new UTF8Encoding();
                var bytes = encoding.GetBytes(_parametters);
                writeStream.Write(bytes, 0, bytes.Length);
            }

            return _webRequest;
        }
    }
}